import { AreaListReturnProps } from '../../../../API';
//更新
function updateTreeData(list: any[], key: React.Key, children: any[]): any[] {
  return list.map((node) => {
    if (node.id === key) {
      return {
        ...node,
        label: node.name,
        value: node.id,
        isLeaf: node.isRegion,
        children,
      };
    } else if (node.children) {
      return {
        ...node,
        label: node.name,
        value: node.id,
        isLeaf: node.isRegion,
        children: updateTreeData(node.children, key, children),
      };
    }
    return node;
  });
}
const arrayToTree = (list: AreaListReturnProps[]) => {
  //转换数据格式 areaPath
  let currentAreaList: any[] = [];
  list.forEach((item: AreaListReturnProps, index: number) => {
    if (index === 0) {
      currentAreaList = item?.listArea || [];
    } else {
      currentAreaList = updateTreeData(
        currentAreaList,
        Number(item.parentId),
        item?.listArea || [],
      );
    }
  });
  return currentAreaList;
};

export default arrayToTree;
